package com.example.dnfgamercommunitybackend.controller;

import com.example.dnfgamercommunitybackend.service.FavoriteArticleService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
public class FavoriteArticleController {
    @Resource
    private FavoriteArticleService favoriteArticleService;

    @PreAuthorize("hasAuthority('user:add_favorite_article')")
    @GetMapping("/favorite/addFavoriteArticle")
    public String addFavoriteArticle(@RequestParam("articleId") int articleId, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        return favoriteArticleService.addFavoriteArticle(articleId,token);
    }

    @PreAuthorize("hasAuthority('user:favorite_get_favorite_list')")
    @GetMapping("/favorite/getUserFavoriteList")
    public String getFavoriteListByUserId(HttpServletRequest request){
        return favoriteArticleService.getFavoriteListByUserId(request.getHeader("Authorization"));
    }

    @PreAuthorize("hasAuthority('user:favorite_cancel_favorite_article')")
    @GetMapping("/favorite/cancelFavoriteArticle")
    public String cancelFavoriteArticle(@RequestParam("id") int id, HttpServletRequest request){
        return favoriteArticleService.cancelFavoriteArticle(id,request.getHeader("Authorization"));
    }
}
